import { Body, Controller, Request, Post } from '@nestjs/common';
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
import { UserService } from './user.service';
import { LoginDto, RegisterDto } from './dto';
import { getClientInfo } from './utils/ua';

@ApiTags('用户')
@Controller('/system')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @ApiOperation({
    summary: '用户登陆',
  })
  @ApiBody({
    type: LoginDto,
    required: true,
  })
  @Post('/login')
  async login(@Body() user: LoginDto, @Request() req) {
    const clientInfo = await getClientInfo(req);
    return await this.userService.login(user, clientInfo);
  }

  @ApiOperation({
    summary: '用户注册',
  })
  @ApiBody({
    type: RegisterDto,
    required: true,
  })
  @Post('/register')
  async register(@Body() user: RegisterDto, @Request() req) {
    const clientInfo = await getClientInfo(req);
    return await this.userService.register(user, clientInfo);
  }
}
